﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AAA.DataTransfer
{
    public class SubstringTranslate : AbstractTranslate
    {
        public SubstringTranslate(int iStart) 
            : this(iStart, -1)
        {
        }

        public SubstringTranslate(int iStart, ITranslate translate)
            : this(iStart, -1, translate)
        {
        }

        public SubstringTranslate(int iStartIndex, int iLength)
            : this(iStartIndex, iLength, null)
        {
        }



        public SubstringTranslate(int iStartIndex, int iLength, ITranslate translate)
            : base(translate)
        {
            StartIndex = iStartIndex;
            Length = iLength;
        }

        private int _iStartIndex;

        public int StartIndex
        {
            get { return _iStartIndex; }
            set { _iStartIndex = value; }
        }
        private int _iLength = -1;

        public int Length
        {
            get { return _iLength; }
            set { _iLength = value; }
        }

        protected override object TryTranslate(object oValue)
        {
            return oValue == null
                    ?   null
                    :   _iLength < 0
                            ? oValue.ToString().Substring(StartIndex)
                            : oValue.ToString().Substring(StartIndex, Length);
        }
    }
}
